#!/bin/bash

# MySQL Management Script for k3d

set -e  # Exit on any error

# Set kubeconfig
export KUBECONFIG=/Users/zhengxin/.k3d/kubeconfig-mycluster.yaml

# Function to check if cluster exists
check_cluster() {
    if ! k3d cluster list | grep -q mycluster; then
        echo "Error: k3d cluster 'mycluster' not found."
        echo "Please deploy the cluster first using deploy-mysql-k3d.sh"
        exit 1
    fi
}

# Function to check if MySQL is deployed
check_mysql_deployment() {
    if ! kubectl get pods -l app=mysql &> /dev/null; then
        echo "Error: MySQL deployment not found."
        echo "Please deploy MySQL first using deploy-mysql-k3d.sh"
        exit 1
    fi
}

# Function to get MySQL pod name
get_mysql_pod() {
    kubectl get pods -l app=mysql -o jsonpath='{.items[0].metadata.name}'
}

# Display menu
show_menu() {
    echo "=== MySQL Management Menu ==="
    echo "1. Check MySQL pod status"
    echo "2. Connect to MySQL (within cluster)"
    echo "3. View MySQL data in k3d container"
    echo "4. Test MySQL persistence"
    echo "5. View service information"
    echo "6. View port mapping"
    echo "7. Delete MySQL deployment"
    echo "8. Delete k3d cluster"
    echo "0. Exit"
    echo "=============================="
}

# Main loop
while true; do
    show_menu
    read -p "Enter your choice (0-8): " choice
    
    case $choice in
        1)
            check_cluster
            echo "=== MySQL Pod Status ==="
            kubectl get pods -l app=mysql
            ;;
        2)
            check_cluster
            check_mysql_deployment
            echo "=== Connecting to MySQL ==="
            kubectl exec -it $(get_mysql_pod) -- mysql -u root -prootpassword
            ;;
        3)
            check_cluster
            echo "=== MySQL Data in k3d Container ==="
            docker exec -it k3d-mycluster-server-0 ls -la /tmp/k3d-mysql-data
            ;;
        4)
            check_cluster
            check_mysql_deployment
            echo "=== Testing MySQL Persistence ==="
            kubectl exec -it $(get_mysql_pod) -- mysql -u root -prootpassword -e "USE mydatabase; CREATE TABLE IF NOT EXISTS test_table (id INT, name VARCHAR(255)); INSERT INTO test_table VALUES (1, 'test'); SELECT * FROM test_table;"
            ;;
        5)
            check_cluster
            echo "=== Service Information ==="
            kubectl get svc
            ;;
        6)
            check_cluster
            echo "=== Port Mapping ==="
            docker port k3d-mycluster-serverlb
            ;;
        7)
            check_cluster
            echo "=== Deleting MySQL Deployment ==="
            kubectl delete -f mysql-local-dev.yaml
            ;;
        8)
            echo "=== Deleting k3d Cluster ==="
            k3d cluster delete mycluster
            ;;
        0)
            echo "Exiting..."
            break
            ;;
        *)
            echo "Invalid choice. Please enter a number between 0-8."
            ;;
    esac
    
    echo ""
    read -p "Press Enter to continue..."
    clear
done